package com.sz.controller;

import com.sz.pojo.RevExp;
import com.sz.pojo.User;
import com.sz.service.RevExpSVImpl;
import com.sz.service.UserSVImpl;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/query")
public class QueryController {
    @Resource
    private RevExpSVImpl revExpSV;
    @Resource
    private UserSVImpl userSV;
    @Resource
    private RedisTemplate redisTemplate;

    @RequestMapping("/all2")
    public String queryAll2(Model model, int page,HttpServletRequest req){
        //向redis存入查询条件
        redisTemplate.opsForValue().set("condition","");
        //上一页翻过头
        if (page==-1) page=0;
        //分页查询
        List<RevExp> records = revExpSV.queryAll2(page);
        //下一页翻过头
        if ((records==null || records.isEmpty()) && page!=0){
            records = revExpSV.queryAll2(--page);
        }
        model.addAttribute("records",records);
        model.addAttribute("pagenum",page+1);
        return "forward:/toIndex";
    }

    @RequestMapping("/all0")
    public String queryAll0(Model model,int page,HttpServletRequest req){
        //向redis存入查询条件
        redisTemplate.opsForValue().set("condition","");
        //上一页翻过头
        if (page==-1) page=0;
        List<RevExp> records = revExpSV.queryAll0(page);
        //下一页翻过头
        if ((records==null || records.isEmpty()) && page!=0){
            records = revExpSV.queryAll0(--page);
        }
        model.addAttribute("records",records);
        model.addAttribute("pagenum",page+1);
        return "forward:/toIndex";
    }

    @RequestMapping("/some0")
    public String querySome0(Model model,String queryContent,int page,HttpServletRequest req){
        if (queryContent.equals("LASTCONDITION"))   queryContent = (String) redisTemplate.opsForValue().get("condition");
        //向redis存入查询条件
        redisTemplate.opsForValue().set("condition",queryContent);
        //上一页翻过头
        if (page==-1) page=0;
        List<RevExp> records = revExpSV.querySome0(queryContent,page);
        //下一页翻过头
        if ((records==null || records.isEmpty()) && page!=0){
            records = revExpSV.querySome0(queryContent,--page);
        }
        model.addAttribute("records",records);
        model.addAttribute("pagenum",page+1);
        return "forward:/toIndex";
    }

    @RequestMapping("/some2")
    public String querySome2(Model model,String queryContent,HttpServletRequest req,int page){
        if (queryContent.equals("LASTCONDITION"))   queryContent = (String) redisTemplate.opsForValue().get("condition");
        //向redis存入查询条件
        redisTemplate.opsForValue().set("condition",queryContent);
        //上一页翻过头
        if (page==-1) page=0;
        //条件查询
        List<RevExp> records = revExpSV.querySome2(queryContent,page);
        //下一页翻过头
        if ((records==null || records.isEmpty()) && page!=0){
            records = revExpSV.querySome2(queryContent,--page);
        }
        model.addAttribute("records",records);
        model.addAttribute("pagenum",page+1);
        return "forward:/toIndex";
    }

    @RequestMapping("/choose")
    public String queryByChoose(String adder,int type,Model model){
        String sql = "where 1=1 ";
        if (!adder.equals("all")){
            sql += "and adder = '" + adder +"'";
        }
        if (type!=2){
            sql += "and type = " + type;
        }
        List<RevExp> records = revExpSV.getByChoose(sql);
        model.addAttribute("records",records);
        return "forward:/toIndex2";
    }
}
